  package com.zprmb.udzt.fragment;

  import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zprmb.udzt.LoginActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.adapter.Contact_Adapter2;
import com.zprmb.udzt.adapter.V6BbtAdapter;
import com.zprmb.udzt.bean.ProductZT;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.view.ListViewForScrollView;

import org.json.JSONObject;

import java.util.ArrayList;

public class ProductFragment2 extends BaseFragment  implements OnRefreshListener2<ScrollView>{

	private ListViewForScrollView lv_product_fragment;
    private PullToRefreshScrollView sv;
//	private ListView lv_product_fragment;
	private ArrayList<ProductZT> list = new ArrayList<ProductZT>();
	private ArrayList<ProductZT> list2 = new ArrayList<ProductZT>();
	private Contact_Adapter2 adapter;
	private V6BbtAdapter adapter2;
	private int page=1;
	Message message;
	private ImageView img_nowork;
	int a=0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.product_fragment, container,
				false);
		
		sv=(PullToRefreshScrollView)view.findViewById(R.id.sv);
		lv_product_fragment = (ListViewForScrollView) view.findViewById(R.id.lv_product_fragment);
//		lv_product_fragment = lv_product_fragment.getRefreshableView();
		registerForContextMenu(lv_product_fragment);
		lv_product_fragment.setFooterDividersEnabled(false);
		
		lv_product_fragment.addFooterView(getFooter(),null,false);
		
		img_nowork=(ImageView)view.findViewById(R.id.iv_v4_nonetwork);

/*
		img_nowork.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initData();
			}
		});
		sv.setOnRefreshListener(this);
		sv.setMode(PullToRefreshBase.Mode.BOTH);


		initData();

 */
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {

	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
	}

	
	private void initData() {

			VolleryUtil volleryUtil=new VolleryUtil() {
				
				@Override
				public void onUpDate(JSONObject jsonObject) {
					// TODO Auto-generated method stub
					Log.d(jsonObject.toString(),"3333333333333333333");
					lv_product_fragment.setVisibility(View.VISIBLE);
					img_nowork.setVisibility(View.GONE);
					
					if(jsonObject.optInt("success")==1){
						LogUtils.e(getActivity(), jsonObject.toString());
						Gson gson=new Gson();
						if (!jsonObject.optString("data").equals("null")) {
							list=gson.fromJson(jsonObject.optString("data"), new TypeToken<ArrayList<ProductZT>>() {
								}.getType());
							
						}
						list2.addAll(list);
						
						adapter2=new V6BbtAdapter(getActivity(), list2);
						
						lv_product_fragment.setAdapter(adapter2);
						adapter2.notifyDataSetChanged();
						
						
						
					lv_product_fragment.setSelection(list2.size()-5);
					}else {
						if (jsonObject.optString("comment").equals("please login")) {
							Intent intent = new Intent(getActivity(),LoginActivity.class);
							startActivity(intent);
						}else{
							Toast.makeText(getActivity(), jsonObject.optString("comment"), 2000).show();
						}
					}
					
				}
				
				@Override
				public void onError() {
					// TODO Auto-generated method stub
					lv_product_fragment.setVisibility(View.GONE);
					img_nowork.setVisibility(View.VISIBLE);
					Toast.makeText(getActivity(), "请求失败   请检查网络设置", 2000).show();
				}
			};
			
	
			volleryUtil.setRequestMethod(0);
			volleryUtil.setShow(true);
			
			volleryUtil.execute(getActivity(), Constant.URL+"/api/v5/list/bbtList?pageNo="+page+"&pageSize=5", "");
		  Log.e("bbt", Constant.URL+"/api/v5/list/bbtList?pageNo="+page+"&pageSize=10");
		
		}
		
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1;
				list2.clear();
				initData();
			}else{
				page+=1;
				initData();
				
				
			}
//			Toast.makeText(getActivity(), "刷新成功", 2000).show();
			sv.onRefreshComplete();
//			postDelayed(new Runnable() {
//				
//				@Override
//				public void run() {
//					// TODO Auto-generated method stub
//					setScaleAdapter();
//				}
//			}, 500);
		};
	};
   
	public static ProductFragment2 newInstance(int position, String string) {
		ProductFragment2 uf = new ProductFragment2();
		
		
		return uf;
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
		
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
		// TODO Auto-generated method stub
		
		handler.sendEmptyMessage(2);
	}

	
  
}
